package database;

import entity.Subject;
import manager.SubjectManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Noi
 */
public class SubjectManagerDB {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public SubjectManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }

    public void loadData(SubjectManager subjectsManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetSubject}");
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int subjectID = rs.getInt("SubjectID");
                int classRoomBlockID = rs.getInt("ClassRoomBlockID");
                String subjectName = rs.getString("SubjectName");
                int classPeriod = rs.getInt("ClassPeriod");
                int pointTypeID = rs.getInt("PointTypeID");
                boolean status = rs.getBoolean("Status");
                Subject subject = new Subject(subjectID, classRoomBlockID, subjectName, classPeriod, pointTypeID, status);
                subjectsManager.add(subject);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SubjectManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteSubject(Subject obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteSubject(?)}");
            cstmt.setInt(1, obj.getSubjectID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SubjectManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void insertSubject(Subject obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertSubject(?,?,?,?,?)}");
            cstmt.registerOutParameter(1, Types.INTEGER);
            cstmt.setInt(2, obj.getClassRoomBlockID());
            cstmt.setString(3, obj.getSubjectName());
            cstmt.setInt(4, obj.getClassPeriod());
            cstmt.setInt(5, obj.getPointTypeID());
            cstmt.executeUpdate();
            obj.setSubjectID(cstmt.getInt("SubjectID"));
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SubjectManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateSubjects(Subject obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateSubject(?,?,?,?,?,?)}");
            cstmt.setInt(1, obj.getSubjectID());
            cstmt.setInt(2, obj.getClassRoomBlockID());
            cstmt.setString(3, obj.getSubjectName());
            cstmt.setInt(4, obj.getClassPeriod());
            cstmt.setInt(5, obj.getPointTypeID());
            cstmt.setBoolean(6, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SubjectManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
